/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.icub3d.nephiteexchange.client;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ExchangesPanel extends VerticalPanel implements ClickHandler,
		ProcessJSONRequest, KeyPressHandler {
	/* These are used for the form. */
	TextBox searchBox = new TextBox();
	Button searchButton = new Button("Search");
	Button resetButton = new Button("Reset");
	HorizontalPanel formPanel = new HorizontalPanel();

	Label errorLabel = null;

	/* The flex table that lists the searched exchange rates. */
	FlexTable exchangeTable = new FlexTable();

	/* These are going to be used for obtaining data. */
	JSONRequest requestor = null;

	public ExchangesPanel(Label errorLabel) {
		super();

		this.errorLabel = errorLabel;

		requestor = new JSONRequest(this.errorLabel, this);

		formPanel.add(searchBox);
		searchBox.addStyleName("searchBox");
		formPanel.add(searchButton);
		searchButton.addStyleName("searchButton");
		formPanel.add(resetButton);
		resetButton.addStyleName("resetButton");
		formPanel.addStyleName("exchangeFormPanel");
		add(formPanel);

		exchangeTable.addStyleName("exchangeTable");
		exchangeTable.setCellPadding(5);
		setTableHeader();
		add(exchangeTable);

		searchBox.addKeyPressHandler(this);
		searchButton.addClickHandler(this);
		resetButton.addClickHandler(this);

		setStyleName("exchangePanel");
		
		requestor.RequestJSON("search/RESET");
	}

	protected void setTableHeader() {
		exchangeTable.setText(0, 0, "Country");
		exchangeTable.setText(0, 1, "Currency Name");
		exchangeTable.setText(0, 2, "Short Name (ISO)");
		exchangeTable.setText(0, 3, "Exchange Rate");
		exchangeTable.getRowFormatter().addStyleName(0, "exchangeTableHeader");
		exchangeTable.getCellFormatter().addStyleName(0, 3, "exchangeTableNumericColumn");
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == searchButton) {
			process("search/" + searchBox.getText());
		} else if (event.getSource() == resetButton) {
			process("search/RESET");
		}
	}

	@Override
	public void processJSONRequest(String response) {
		JsArray<ExchangeRate> ers = asArrayOfExchangeRate(response);

		while (exchangeTable.getRowCount() > 1) {
			exchangeTable.removeRow(1);
		}
		
		String color = "light";

		NumberFormat nf = NumberFormat.getFormat("########0.00000");
		
		for (int i = 0; i < ers.length(); i++) {
			exchangeTable.setText(i + 1, 0, ers.get(i).getCountry());
			exchangeTable.setText(i + 1, 1, ers.get(i).getCurrency());
			exchangeTable.setText(i + 1, 2, ers.get(i).getISOName());
			exchangeTable.setText(i + 1, 3, nf.format(ers.get(i).getExchange()));
			exchangeTable.getCellFormatter().addStyleName(i + 1, 3, "exchangeTableNumericColumn");
			exchangeTable.getRowFormatter().addStyleName(i + 1, color + "TableBackground");
			
			color = (color == "light" ? "dark" : "light");
				
		}

	}

	public void process(String request) {
		requestor.RequestJSON(request);
		searchBox.setText("");
	}

	private final native JsArray<ExchangeRate> asArrayOfExchangeRate(String json) /*-{
		return eval(json);
	}-*/;

	@Override
	public void onKeyPress(KeyPressEvent event) {
		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
			process("search/" + searchBox.getText());
		}
	}
}
